package com.example.mall.web.controller;

import com.example.mall.domain.CouponProductRelation;
import com.example.mall.dto.CouponProductRelationDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.CouponProductRelationQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ICouponProductRelationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/couponProductRelation")
public class CouponProductRelationController {

    @Autowired
    public ICouponProductRelationService couponProductRelationService;

    /**
     * 分页查询优惠券适用商品表信息
     * @param couponProductRelationQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询优惠券适用商品表信息",notes = "分页查询优惠券适用商品表信息",httpMethod = "GET")
    public JSONResult queryPage(CouponProductRelationQuery couponProductRelationQuery){
        PageDTO<CouponProductRelationDto> couponProductRelationDtoPageDTO = couponProductRelationService.queryPage(couponProductRelationQuery);
        return JSONResult.success("查询成功",couponProductRelationDtoPageDTO);
    }

    /**
     * 根据id查询优惠券适用商品表信息
     * @param relationId
     * @return
     */
    @GetMapping("query/{relationId}")
    @ApiOperation(value = "根据id查询优惠券适用商品表信息",notes = "根据id查询优惠券适用商品表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("relationId")Long relationId){
        //1.根据id查询用户信息
        if (relationId == null){
            throw new GlobalCustomerException(ErrorEnumCode.RELATIONID_NOT_ID);
        }
        CouponProductRelation couponProductRelation = couponProductRelationService.getById(relationId);
        return JSONResult.success("查询成功",couponProductRelation);
    }

    /**
     * 根据id查询优惠券适用商品表信息
     * @param relationId
     * @return
     */
    @GetMapping("queryCouponProductRelationById/{relationId}")
    @ApiOperation(value = "根据id查询优惠券适用商品表信息",notes = "根据id查询优惠券适用商品表信息",httpMethod = "GET")
    public JSONResult queryCouponProductRelationById(@PathVariable("relationId")Long relationId){
        //1.根据id查询用户信息
        if (relationId == null){
            throw new GlobalCustomerException(ErrorEnumCode.RELATIONID_NOT_ID);
        }
        CouponProductRelation couponProductRelation = couponProductRelationService.queryCouponProductRelationById(relationId);
        return JSONResult.success("查询成功",couponProductRelation);
    }

    /**
     * 查询全部优惠券适用商品表信息
     * @param
     * @return
     */
    @GetMapping("queryCouponProductRelationList")
    @ApiOperation(value = "查询全部优惠券适用商品表信息",notes = "查询全部优惠券适用商品表信息",httpMethod = "GET")
    public JSONResult queryCouponProductRelationList(){
        List<CouponProductRelation> couponProductRelations = couponProductRelationService.queryCouponProductRelationList();
        return JSONResult.success("查询成功",couponProductRelations);
    }

    /**
     * 新增优惠券适用商品表
     * @param couponProductRelationDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增优惠券适用商品表",notes = "新增优惠券适用商品表",httpMethod = "POST")
    public JSONResult insert(@RequestBody CouponProductRelationDto couponProductRelationDto){
        couponProductRelationService.insert(couponProductRelationDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改优惠券适用商品表信息
     * @param couponProductRelationDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改优惠券适用商品表信息",notes = "修改优惠券适用商品表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody CouponProductRelationDto couponProductRelationDto){
        couponProductRelationService.update(couponProductRelationDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除优惠券适用商品表信息
     * @param relationId
     * @return
     */
    @PostMapping("delete/{relationId}")
    @ApiOperation(value = "删除优惠券适用商品表信息",notes = "删除优惠券适用商品表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("relationId") Long relationId) {
        couponProductRelationService.delete(relationId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除优惠券适用商品表信息
     * @param relationIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除优惠券适用商品表信息",notes = "批量删除优惠券适用商品表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> relationIds) {
        couponProductRelationService.deleteBatch(relationIds);
        return JSONResult.success("批量删除成功");
    }
}
